package com.cq.hd.product.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.cq.hd.common.enums.biz.GroupCreatorPermissionEnum;
import com.cq.hd.common.enums.biz.PromotionTypeEnum;
import com.cq.hd.common.response.Throw;
import com.cq.hd.member.api.TbBusinessAgentApi;
import com.cq.hd.member.api.vo.BusinessAgentVo;
import com.cq.hd.product.api.dto.GroupBuyActiveReleaseDto;
import com.cq.hd.product.mapper.TbActiveGroupBuyMapper;
import com.cq.hd.product.po.TbActiveGroupBuyPo;
import com.cq.hd.product.service.TbActiveGroupBuyService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Collections;
import java.util.Date;
import java.util.List;

/**
 * @author much
 * @since 2024/09/04 22:02
 */
@Slf4j
@Service
public class TbActiveGroupBuyServiceImpl implements TbActiveGroupBuyService {

    @Resource
    private TbActiveGroupBuyMapper tbActiveGroupBuyMapper;
    @Resource
    private TbBusinessAgentApi businessAgentApi;

    @Override
    public void saveGroupBuyActive(GroupBuyActiveReleaseDto groupBuyActiveReleaseDto) {
        if (!PromotionTypeEnum.GROUP_BUY.name().equals(groupBuyActiveReleaseDto.getPromotionType())) {
            log.error("活动发布-拼团：营销类型错误");
            return;
        }
        if (groupBuyActiveReleaseDto.getGroupNum() < 2) {
            Throw.isBusinessException("成团人数不可低于2人");
        }

        TbActiveGroupBuyPo tbActiveGroupBuyPo = queryGroupBuyActiveByActiveId(groupBuyActiveReleaseDto.getId());
        if (tbActiveGroupBuyPo == null) {
            tbActiveGroupBuyPo = BeanUtil.copyProperties(groupBuyActiveReleaseDto, TbActiveGroupBuyPo.class);
            tbActiveGroupBuyPo.setActiveId(groupBuyActiveReleaseDto.getId());
            tbActiveGroupBuyPo.setCreateTime(new Date());
            tbActiveGroupBuyMapper.insert(tbActiveGroupBuyPo);
        } else {
            TbActiveGroupBuyPo updater = BeanUtil.copyProperties(groupBuyActiveReleaseDto, TbActiveGroupBuyPo.class);
            updater.setId(tbActiveGroupBuyPo.getId());
            updater.setUpdateTime(new Date());
            tbActiveGroupBuyMapper.updateById(updater);
        }
    }

    @Override
    public List<TbActiveGroupBuyPo> listByActiveIds(List<Long> activeIds) {
        if (CollectionUtils.isEmpty(activeIds)) {
            return Collections.emptyList();
        }
        LambdaQueryWrapper<TbActiveGroupBuyPo> qw = Wrappers.<TbActiveGroupBuyPo>lambdaQuery()
                .in(TbActiveGroupBuyPo::getActiveId, activeIds);
        return tbActiveGroupBuyMapper.selectList(qw);
    }

    @Override
    public TbActiveGroupBuyPo getByActiveId(Long activeId) {
        LambdaQueryWrapper<TbActiveGroupBuyPo> qw = Wrappers.<TbActiveGroupBuyPo>lambdaQuery()
                .eq(TbActiveGroupBuyPo::getActiveId, activeId)
                .orderByDesc(TbActiveGroupBuyPo::getId)
                .last("LIMIT 1");
        return tbActiveGroupBuyMapper.selectOne(qw);
    }

    @Override
    public Boolean hasCreateGroupPermission(Long activeId, Long userId) {
        TbActiveGroupBuyPo activeGroupBuyPo = getByActiveId(activeId);
        if (GroupCreatorPermissionEnum.LEVEL1_AGENT.name().equals(activeGroupBuyPo.getGroupCreatorPermission())) {
            BusinessAgentVo businessAgentVo = businessAgentApi.getBusinessAgentById(activeGroupBuyPo.getBusinessId(), userId).unpack();
            return businessAgentVo != null;
        }
        return true;
    }

    private TbActiveGroupBuyPo queryGroupBuyActiveByActiveId(Long activeId) {
        if (activeId != null) {
            LambdaQueryWrapper<TbActiveGroupBuyPo> qw = Wrappers.<TbActiveGroupBuyPo>lambdaQuery()
                    .eq(TbActiveGroupBuyPo::getActiveId, activeId);
            return tbActiveGroupBuyMapper.selectOne(qw);
        }
        return null;
    }

}
